package com.soul.dj.util.http;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import com.soul.dj.ui.activity.BaseActivity;
import com.soul.dj.ui.fragment.BaseFragment;

import java.util.Map;

/**
 * @author
 * @ClassNmae MainHandler
 * @create 2018-09-19 16:27
 **/
public class MainHandler extends Handler {

    private Object context;
    public MainHandler(Object context) {
        this.context =context;
    }

    // 子类必须重写此方法，接受数据
    @Override
    public void handleMessage(Message msg) {
        Map<String,Object> map = (Map<String, Object>) msg.obj;
        switch (msg.what) {
            case Activity.RESULT_OK:
                if ( context instanceof BaseFragment){
                    BaseFragment baseFragment = (BaseFragment) context;
                    baseFragment.onAjaxOK(map.get("result"),map.get("tag").toString());
                }else  if ( context instanceof BaseActivity){
                    BaseActivity baseActivity = (BaseActivity) context;
                    baseActivity.onAjaxOK(map.get("result"),map.get("tag").toString());
                }
                break;
            case Activity.RESULT_CANCELED:

                if ( context instanceof BaseFragment){
                    BaseFragment baseFragment = (BaseFragment) context;
                    baseFragment.onAjaxFail(map.get("code").toString(),map.get("title").toString(),map.get("str").toString(),map.get("tag").toString());
                }else  if ( context instanceof BaseActivity){
                    BaseActivity baseActivity = (BaseActivity) context;
                    baseActivity.onAjaxFail(map.get("code").toString(),map.get("title").toString(),map.get("str").toString(),map.get("tag").toString());
                }

                break;
            case Activity.RESULT_FIRST_USER:
                if ( context instanceof BaseFragment){
                    BaseFragment baseFragment = (BaseFragment) context;
                    baseFragment.onAjaxError(map.get("tag").toString());
                }else  if ( context instanceof BaseActivity){
                    BaseActivity baseActivity = (BaseActivity) context;
                    baseActivity.onAjaxError(map.get("tag").toString());
                }

                break;
            default:
                super.handleMessage(msg);
        }
    }
}